﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Infrastructure;
using System.Web.Security;
using Utils;

namespace WebUI.Models
{
    public class UserIdentityModel
    {
        private Boolean _bLogOnPartialView;
        public UserIdentityModel(Boolean bLogOnPartialView = true)
        {
            _bLogOnPartialView = bLogOnPartialView;
            Init();
        }
        
        public int LoginID { get; private set; }
        public int AccountID { get; private set; }
        public string AccountType { get; private set; }

        public Boolean IsLoggedIn { get; private set; }
        public Boolean IsIndLoggedIn { get; private set; }
        public Boolean IsCoLoggedIn { get; private set; }

        public string UserName { get; private set; }

        private void Init()
        {
            //loginID, AccountID, AccountType from cookie
            try
            {
                if (HttpContext.Current.User != null &&
                    HttpContext.Current.User.Identity != null &&
                    HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    string[] authCookieString = HttpContext.Current.User.Identity.Name.Split('|');
                    LoginID = Convert.ToInt32(authCookieString[0]);
                    AccountID = Convert.ToInt32(authCookieString[1]);
                    AccountType = authCookieString[2];
                }
                else
                {
                    ResetVars();
                }

            }
            catch
            {
                ResetVars();
            }
            //IsLoggedIn
            IsLoggedIn = LoginID > 0;
            IsIndLoggedIn = IsLoggedIn && AccountType == "i";
            IsCoLoggedIn = IsLoggedIn && AccountType == "c";
            //get UserName from db, since UserName can be changed
            if (IsLoggedIn && _bLogOnPartialView)
            {
                UserName = SqlUtilities.Query<string>(ConstUtils.SP_GetUserName, new { LoginID = LoginID }).FirstOrDefault();
                //if can't get username, means this is no longer a valid cookie
                if (string.IsNullOrEmpty(UserName))
                {
                    ResetVars();
                    FormsAuthentication.SignOut();
                }
            }
        }

        private void ResetVars()
        {
            LoginID = 0;
            AccountID = 0;
            AccountType = "";
            IsLoggedIn = false;
            
        }

        //public method
        public Boolean IsAccountOwner(string userName) 
        {
            if (IsLoggedIn &&
                !string.IsNullOrEmpty(this.UserName) &&
                !string.IsNullOrEmpty(userName) &&
                userName.ToLower() == this.UserName.ToLower()
                )
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Boolean IsIndAccountOwner(string userName)
        {
            return IsAccountOwner(userName) && IsIndLoggedIn;
        }
    }
}